import tkinter as tk 
import sys
import os
import re
script_dir = os.path.abspath(sys.argv[0])
work_dir = '/'.join(script_dir.split('\\')[:-1])
sys.path.append(work_dir)

import time_recorder


window = tk.Tk() # * 实例化tk对象

window.title('我的一生')

window.geometry('500x300')

tk.Label(window, text='事件', font=('Arial', 14)).place(x=10, y=20)
tk.Label(window, text='发生时间', font=('Arial', 14)).place(x=10, y=60)
tk.Label(window, text='结束时间', font=('Arial', 14)).place(x=10, y=100)



# * 输入事件
var_event = tk.StringVar()
entry_event = tk.Entry(window, textvariable=var_event, font=('Arial', 14))
entry_event.place(x=120, y=20)
# * 输入开始时间
var_start_time = tk.StringVar()
entry_start_time = tk.Entry(window, textvariable=var_start_time, font=('Arial', 14))
entry_start_time.place(x=120, y=60)
# * 输入结束时间
var_end_time = tk.StringVar()
entry_end_time = tk.Entry(window, textvariable=var_end_time, font=('Arial', 14))
entry_end_time.place(x=120, y=100)

# * 整理输入的时间格式
def date_style(x):
    date_list = re.findall(r'\d{1,2}', str(x))
    date = ':'.join(date_list)
    return date



# * button的功能
def recorder():
    event = var_event.get()
    start_time = date_style(var_start_time.get())
    end_time = date_style(var_end_time.get())

    time_recorder.run2(event, start_time, end_time)
    print("事件: {}, 开始时间: {}, 结束时间: {}".format(event, start_time, end_time))

btn_record = tk.Button(window, text='录入', command=recorder)
btn_record.place(x=120, y=140)

# * 清除所有内容
def clear():
    entry_event.delete(0, tk.END)
    entry_start_time.delete(0, tk.END)
    entry_end_time.delete(0, tk.END)


btn_clean = tk.Button(window, text='下一条', command=clear)
btn_clean.place(x=180, y=140)

window.mainloop()